package bs

import net.liftweb.common.{Empty, Full, Box}
import scala.xml.NodeSeq
import net.liftweb.http.Templates
import scala.collection.mutable

import net.liftweb._
import http._
import util._
import Helpers._

case class Alert(content: String) extends LiftComponent {
  val styles = mutable.Buffer("alert")

  var _title: Box[String] = Empty
  var hasCloseBtn = false

  def title(t: String) = {
    _title = Full(t)
    this
  }

  def withCloseBtn = {
    hasCloseBtn = true
    this
  }

  def error = {
    styles += "alert-error"
    this
  }

  def success = {
    styles += "alert-success"
    this
  }

  def danger = {
    styles += "alert-danger"
    this
  }

  def info = {
    styles += "alert-info"
    this
  }

  def block = {
    styles += "alert-block"
    this
  }

  override val template: NodeSeq = <div class="alert"></div> //Templates("templates-hidden"::"bs"::"_alert"::Nil).get

  override def transformer: (NodeSeq) => NodeSeq = {
    val setStyle = ".alert [class]" #> styles.mkString(" ")
    val setContent = ".alert *<" #> content
    import code.lib.ExtCssSel._
    setContent & setStyle &?
      hasCloseBtn -> ".alert >*" #> <button type="button" class="close" data-dismiss="alert">&times;</button> &?
      _title.map(title => ".alert >*" #> <h4>{title}</h4>)
  }
}
